package com.itheima.area.controller;

import com.github.pagehelper.Page;
import com.itheima.area.annotation.MultiRequestBody;
import com.itheima.area.pojo.CustFarmland;
import com.itheima.area.pojo.CustFarmlandCrop;
import com.itheima.area.pojo.CustFarmlandDevice;
import com.itheima.area.service.FieldService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @PackageName com.itheima.area.controller
 * @ProjectName Ceres-Work
 * @Created by Chanly on 2020/10/14 9:09
 **/

@RequestMapping("/field")
@RestController
public class FieldController {
    @Autowired
    private FieldService fieldService ;

    /**
     * 根据Id查询
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findAll(@PathVariable Long id){
        try {
            List<CustFarmland> list = fieldService.findAll(id);
            return  new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
   @DeleteMapping(value = "{id}")
   public Result delete(@PathVariable Long id){
       try {
           fieldService.deleteById(id);
           return new Result(true,StatusCode.OK,"删除成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,StatusCode.ERROR,"删除失败");
       }
   }

    /**
     * 添加片区
     * @param custFarmland
     * @param custFarmlandCrop
     * @param custFarmlandDevice
     * @return
     */
    @PostMapping(value="/add")
    public Result add(@MultiRequestBody("custFarmland") CustFarmland custFarmland,@MultiRequestBody("custFarmlandCrop") CustFarmlandCrop custFarmlandCrop ,@MultiRequestBody("custFarmlandDevice") CustFarmlandDevice custFarmlandDevice ){
        try {
            fieldService.add(custFarmland, custFarmlandCrop, custFarmlandDevice);
            return  new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    /**
     * 修改
     * @param custFarmland
     * @param custFarmlandCrop
     * @param custFarmlandDevice
     * @return
     */
    @PutMapping(value = "/updateFarmland/{id}")
    public Result updateFarmland(@MultiRequestBody CustFarmland custFarmland, @MultiRequestBody CustFarmlandCrop custFarmlandCrop, @MultiRequestBody CustFarmlandDevice custFarmlandDevice,@PathVariable Long id){
        try {
            fieldService.update(custFarmland, custFarmlandDevice, custFarmlandCrop,id);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        try {
            Page<CustFarmland> pageList = fieldService.findPage(searchMap, page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }
}
